package com.fd.dx.web.controllers;

import com.fd.dx.domain.ZP;
import com.fd.dx.service.ZPService;
import com.fd.dx.web.domain.ZPTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2018/2/27.
 */
@Controller
@RequestMapping("/zp")
public class ZPController {
    @Autowired
    private ZPService zpService;
    @RequestMapping("/get2D/{id}")
    @ResponseBody
    public ZPTreeNode get2D(@PathVariable("id")Integer id) {
        ZPTreeNode treeNode=new ZPTreeNode();
        ZP zp=zpService.finePersonFromID(id);
        treeNode.setId(zp.getId());
        treeNode.setName(zp.getName());
        treeNode.setHasChild(zp.getHaschild());
        List<ZP> zps=zpService.getChildfromPid(zp.getId());
        List<ZPTreeNode> nodes=new ArrayList<>();
        for(ZP child :zps){
            ZPTreeNode z=new ZPTreeNode();
            z.setId(child.getId());
            z.setName(child.getName());
            z.setHasChild(child.getHaschild());
            z.setIsLoad("no");
            nodes.add(z);
        }
        treeNode.setIsLoad("true");
        treeNode.setChildren(nodes);
        return treeNode;
    }
    @RequestMapping("/addZP")
    @ResponseBody
    public Integer addZP(ZP zp) {
        return zpService.addZP(zp);
    }
    @RequestMapping("/deleteZP")
    @ResponseBody
    public Integer deleteZP(Integer id) {

        return zpService.deleteZP(id);
    }
    @RequestMapping("/updateZP")
    @ResponseBody
    public Integer updateZP(ZP zp) {
        return zpService.updateZP(zp);
    }

}
